/**
 * Copyright 2011 Poltman.Ltd; www.poltman.com. All rights reserved.		
 *
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */
package com.poltman.dscentral.core.webappinit;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.WebApp;

import com.poltman.dscentral.core.ConfigurationManager;
import com.poltman.dscentral.core.CoreStatics;

/**
 * 
 * @author z.ciok@poltman.com
 *
 */
public class WebAppInit implements org.zkoss.zk.ui.util.WebAppInit {

	private static Logger log = Logger.getLogger(WebAppInit.class);
	
	private static WebAppInit instance = null;
	private static WebApp webApp;
	
	private static String defaultLanguage;
	@SuppressWarnings("static-access")
	public String getDefaultLanguage() {
		return this.defaultLanguage;
	}
	@SuppressWarnings("static-access")
	public void setDefaultLanguage(String defaultLanguage) {
		this.defaultLanguage = defaultLanguage;
	}
	
	//------------
	
	private static String dspaceDir;
	public String getDspaceDir() {
		return dspaceDir;
	}
	public void setDspaceDir(String dspaceDir) {
		WebAppInit.dspaceDir = dspaceDir;
	}
	
	//------------
	
	private static String dspaceConfig;
	public static String getDspaceConfig() {
		return dspaceConfig;
	}
	public static void setDspaceConfig(String dspaceConfig) {
		WebAppInit.dspaceConfig = dspaceConfig;
	}
	
	private static String dscentralUrl;
	public static String getDscentralUrl() {
		return dscentralUrl;
	}
	public static void setDscentralUrl(String dscentralUrl) {
		WebAppInit.dscentralUrl = dscentralUrl;
	}
	
	
	//------------ LDAP
	

	private static String ldapAdministrator;
	public static String getLdapAdministrator() {
		return ldapAdministrator;
	}
	public static void setLdapAdministrator(String ldapAdministrator) {
		WebAppInit.ldapAdministrator = ldapAdministrator;
	}
	
	private static String ldapAdministratorPassword;
	public static String getLdapAdministratorPassword() {
		return ldapAdministratorPassword;
	}
	public static void setLdapAdministratorPassword(String ldapAdministratorPassword) {
		WebAppInit.ldapAdministratorPassword = ldapAdministratorPassword;
	}

	private static String ldapProviderUrl;
	public static String getLdapProviderUrl() {
		return ldapProviderUrl;
	}
	public static void setLdapProviderUrl(String ldapProviderUrl) {
		WebAppInit.ldapProviderUrl = ldapProviderUrl;
	}

	private static String ldapIdField;
	public static String getLdapIdField() {
		return ldapIdField;
	}
	public static void setLdapIdField(String ldapIdField) {
		WebAppInit.ldapIdField = ldapIdField;
	}

	private static String ldapSearchContext;
	public static String getLdapSearchContext() {
		return ldapSearchContext;
	}
	public static void setLdapSearchContext(String ldapSearchContext) {
		WebAppInit.ldapSearchContext = ldapSearchContext;
	}

	private static String ldapObjectContext;
	public static String getLdapObjectContext() {
		return ldapObjectContext;
	}
	public static void setLdapObjectContext(String ldapObjectContext) {
		WebAppInit.ldapObjectContext = ldapObjectContext;
	}
	
	public static String ldapEmailField;
	public static String getLdapEmailField() {
		return ldapEmailField;
	}
	public static void setLdapEmailField(String ldapEmailField) {
		WebAppInit.ldapEmailField = ldapEmailField;
	}

	public static String ldapGivennameField;
	public static String getLdapGivennameField() {
		return ldapGivennameField;
	}
	public static void setLdapGivennameField(String ldapGivennameField) {
		WebAppInit.ldapGivennameField = ldapGivennameField;
	}

	public static String ldapSurnameField;
	public static String getLdapSurnameField() {
		return ldapSurnameField;
	}
	public static void setLdapSurnameField(String ldapSurnameField) {
		WebAppInit.ldapSurnameField = ldapSurnameField;
	}

	public static String ldapPhoneField;
	public static String getLdapPhoneField() {
		return ldapPhoneField;
	}
	public static void setLdapPhoneField(String ldapPhoneField) {
		WebAppInit.ldapPhoneField = ldapPhoneField;
	}
	
	//------------------
	
	
	public WebAppInit() {
	}	
	
	public static WebAppInit getInstance() {
		if(instance == null) {
			instance = new WebAppInit();
		}
		return instance;
	}	
	
	@SuppressWarnings({ "static-access"})
	@Override
	public void init(WebApp webApp) throws Exception {
		String mainFilePath = webApp.getRealPath(File.separator) + CoreStatics.MAIN_PROPERTIES_FILE_RELATIVE_PATH;
		Properties pro = new Properties();		
		try {
			FileInputStream in = new FileInputStream(mainFilePath);
			pro.load(in);
			this.defaultLanguage = pro.getProperty("default.language");
			this.dspaceDir = pro.getProperty("dspace.dir");
			this.dspaceConfig = pro.getProperty("dspace.config");
			
			this.dscentralUrl = pro.getProperty("dscentral.url");
			
			this.ldapAdministrator = pro.getProperty("ldapAdministrator");
			this.ldapAdministratorPassword = pro.getProperty("ldapAdministratorPassword");
			this.ldapProviderUrl = pro.getProperty("ldap.provider.url"); 
			this.ldapIdField = pro.getProperty("ldap.id.field");
			this.ldapSearchContext = pro.getProperty("ldap.search.context");
			this.ldapObjectContext = pro.getProperty("ldap.object.context");
			
			this.ldapEmailField = pro.getProperty("ldap.email.field"); 
			this.ldapGivennameField = pro.getProperty("ldap.givenname.field"); 
			this.ldapSurnameField = pro.getProperty("ldap.surname.field"); 
			this.ldapPhoneField	= pro.getProperty("ldap.phone.field"); 
			
		} catch (IOException e) {
			log.error(e);
			throw(e);
		}		
		
        if (!ConfigurationManager.isConfigured()) {
            ConfigurationManager.loadConfig(this.dspaceConfig);
        }
		
		WebAppInit.webApp = webApp;
		log.info("DSCentral WebAppInit... OK");
		log.info("DSCentral WebAppInit, default.language = " + this.defaultLanguage);
		//log.info("DSCentral WebAppInit, dspace.dir = " + this.dspaceDir);
		//log.info("DSCentral WebAppInit, dspace.config = " + this.dspaceConfig);
	}

	public WebApp getWebApp() {
		return webApp;
	}
	
	//--------------------
	
}
